﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iLangl.Model;
using System.Data;

namespace iLangl.BusinessModel.DAL.XMLStorage
{
	public class CompanyDAL : BaseStorageDAL, ICompanyDAL
	{
		#region ICompanyDAL Members

		public void CreateCompany(Company company, IDbTransaction transaction)
		{
			if (company == null)
				return;

			//Check if company exists
			dtoXmlStorage.CompanyRow companyRow = Storage.Company.FindById(company.Id);

			bool isChanged = false;
			bool isNew = (companyRow == null);
			if (isNew)
			{
				companyRow = Storage.Company.NewCompanyRow();
				isChanged = true;
			}

			if (!isChanged)
			{
				isChanged |= !company.Id.Equals(companyRow.Id);
				if ((!companyRow.IsNameNull()) && (!string.IsNullOrEmpty(company.Name)))
					isChanged |= !company.Name.Equals(companyRow.Name);
				if ((!companyRow.IsAddressNull()) && (!string.IsNullOrEmpty(company.Address)))
					isChanged |= !company.Address.Equals(companyRow.Address);
				if ((!companyRow.IsCityNull()) && (!string.IsNullOrEmpty(company.City)))
					isChanged |= !company.City.Equals(companyRow.City);
				if ((!companyRow.IsCountryNull()) && (!string.IsNullOrEmpty(company.Country)))
					isChanged |= !company.Country.Equals(companyRow.Country);
				if ((!companyRow.IsPhoneNull()) && (!string.IsNullOrEmpty(company.Phone)))
					isChanged |= !company.Phone.Equals(companyRow.Phone);
				if ((!companyRow.IsStateNull()) && (!string.IsNullOrEmpty(company.State)))
					isChanged |= !company.State.Equals(companyRow.State);
				if ((!companyRow.IsZipNull()) && (!string.IsNullOrEmpty(company.Zip)))
					isChanged |= !company.Zip.Equals(companyRow.Zip);
				if ((!companyRow.IsCustomerIdentityNull()) && (!string.IsNullOrEmpty(company.CustomerIdentity)))
					isChanged |= !company.CustomerIdentity.Equals(companyRow.CustomerIdentity);
				if ((!companyRow.IsTPIdentityNull()) && (!string.IsNullOrEmpty(company.TPIdentity)))
					isChanged |= !company.TPIdentity.Equals(companyRow.TPIdentity);

			}

			companyRow.Id = company.Id;


			if (!string.IsNullOrEmpty(company.Name))
				companyRow.Name = company.Name;

			if (!string.IsNullOrEmpty(company.Address))
				companyRow.Address = company.Address;

			if (!string.IsNullOrEmpty(company.City))
				companyRow.City = company.City;

			if (!string.IsNullOrEmpty(company.Country))
				companyRow.Country = company.Country;

			if (!string.IsNullOrEmpty(company.Currency))
				companyRow.Currency = company.Currency;

			if (!string.IsNullOrEmpty(company.Phone))
				companyRow.Phone = company.Phone;

			if (!string.IsNullOrEmpty(company.State))
				companyRow.State = company.State;

			if (!string.IsNullOrEmpty(company.Zip))
				companyRow.Zip = company.Zip;

			if (!string.IsNullOrEmpty(company.CustomerIdentity))
				companyRow.CustomerIdentity = company.CustomerIdentity;

			if (!string.IsNullOrEmpty(company.TPIdentity))
				companyRow.TPIdentity = company.TPIdentity;

			if (isNew)
			{
				Storage.Company.AddCompanyRow(companyRow);
			}

			if (isChanged)
			{
				companyRow.TimeSpan = CurrentTimeSpan;
			}

			if (company.BillingInfo != null)
			{
				isChanged = false;

				//Save Billing Info
				dtoXmlStorage.BillingInfoRow billingrow = Storage.BillingInfo.FindById(company.BillingInfo.Id);

				isNew = (billingrow == null);

				if (isNew)
				{
					billingrow = Storage.BillingInfo.NewBillingInfoRow();
					isChanged = true;
				}

				BillingInfo bi = company.BillingInfo;
				if (!isChanged)
				{
					isChanged |= !bi.Id.Equals(billingrow.Id);
					if ((!billingrow.IsCompanyIdNull()) && (!string.IsNullOrEmpty(bi.Id)))
						isChanged |= !company.Id.Equals(billingrow.CompanyId);
					if ((!billingrow.IsNameNull()) && (!string.IsNullOrEmpty(bi.Name)))
						isChanged |= !bi.Name.Equals(billingrow.Name);
					if ((!billingrow.IsBillingFirstNameNull()) && (!string.IsNullOrEmpty(bi.BillingFirstName)))
						isChanged |= !bi.BillingFirstName.Equals(billingrow.BillingFirstName);
					if ((!billingrow.IsBillingLastNameNull()) && (!string.IsNullOrEmpty(bi.BillingLastName)))
						isChanged |= !bi.BillingLastName.Equals(billingrow.BillingLastName);
					if ((!billingrow.IsBillingEmailNull()) && (!string.IsNullOrEmpty(bi.BillingEmail)))
						isChanged |= !bi.BillingEmail.Equals(billingrow.BillingEmail);
					if ((!billingrow.IsBillingCountryNull()) && (!string.IsNullOrEmpty(bi.BillingCountry)))
						isChanged |= !bi.BillingCountry.Equals(billingrow.BillingCountry);
					if ((!billingrow.IsBillingStreetAddressNull()) && (!string.IsNullOrEmpty(bi.BillingStreetAddress)))
						isChanged |= !bi.BillingStreetAddress.Equals(billingrow.BillingStreetAddress);
					if ((!billingrow.IsBillingStreetAddress2Null()) && (!string.IsNullOrEmpty(bi.BillingStreetAddress2)))
						isChanged |= !bi.BillingStreetAddress2.Equals(billingrow.BillingStreetAddress2);
					if ((!billingrow.IsBillingPostalCodeNull()) && (!string.IsNullOrEmpty(bi.BillingPostalCode)))
						isChanged |= !bi.BillingPostalCode.Equals(billingrow.BillingPostalCode);
					if ((!billingrow.IsBillingCityNull()) && (!string.IsNullOrEmpty(bi.BillingCity)))
						isChanged |= !bi.BillingCity.Equals(billingrow.BillingCity);
					if ((!billingrow.IsBillingCurrencyNull()) && (!string.IsNullOrEmpty(bi.BillingCurrency)))
						isChanged |= !bi.BillingCurrency.Equals(billingrow.BillingCurrency);
					if ((!billingrow.IsCustomerIdentityNull()) && (!string.IsNullOrEmpty(bi.CustomerIdentity)))
						isChanged |= !bi.CustomerIdentity.Equals(billingrow.CustomerIdentity);
					if ((!billingrow.IsTPIdentityNull()) && (!string.IsNullOrEmpty(bi.TPIdentity)))
						isChanged |= !bi.TPIdentity.Equals(billingrow.TPIdentity);

				}

				billingrow.Id = bi.Id;
				billingrow.CompanyId = company.Id;

				if (!string.IsNullOrEmpty(bi.Name))
					billingrow.Name = bi.Name;

				if (!string.IsNullOrEmpty(bi.BillingFirstName))
					billingrow.BillingFirstName = bi.BillingFirstName;

				if (!string.IsNullOrEmpty(bi.BillingLastName))
					billingrow.BillingLastName = bi.BillingLastName;

				if (!string.IsNullOrEmpty(bi.BillingEmail))
					billingrow.BillingEmail = bi.BillingEmail;

				if (!string.IsNullOrEmpty(bi.BillingCountry))
					billingrow.BillingCountry = bi.BillingCountry;

				if (!string.IsNullOrEmpty(bi.BillingStreetAddress))
					billingrow.BillingStreetAddress = bi.BillingStreetAddress;

				if (!string.IsNullOrEmpty(bi.BillingStreetAddress2))
					billingrow.BillingStreetAddress2 = bi.BillingStreetAddress2;

				if (!string.IsNullOrEmpty(bi.BillingPostalCode))
					billingrow.BillingPostalCode = bi.BillingPostalCode;

				if (!string.IsNullOrEmpty(bi.BillingCity))
					billingrow.BillingCity = bi.BillingCity;

				if (!string.IsNullOrEmpty(bi.BillingCurrency))
					billingrow.BillingCurrency = bi.BillingCurrency;


				if (!string.IsNullOrEmpty(bi.CustomerIdentity))
					billingrow.CustomerIdentity = bi.CustomerIdentity;

				if (!string.IsNullOrEmpty(bi.TPIdentity))
					billingrow.TPIdentity = bi.TPIdentity;


				if (isNew)
				{
					Storage.BillingInfo.AddBillingInfoRow(billingrow);
				}

				if (isChanged)
				{
					billingrow.TimeSpan = CurrentTimeSpan;
				}

			}

			if (transaction == null)
			{
				AcceptChanges();
			}

		}

		public Company ReadCompany(string companyId)
		{
			dtoXmlStorage.CompanyRow companyRow = Storage.Company.FindById(companyId);

			if (companyRow == null)
				return null;

			Company company = new Company();
			company.Id = companyRow.Id;
			company.Name = companyRow.Name;
			company.Address = companyRow.Address;
			company.City = companyRow.City;
			company.Country = companyRow.Country;
			company.Currency = companyRow.Currency;
			company.Phone = companyRow.Phone;
			company.State = companyRow.State;
			company.Zip = companyRow.Zip;

			if (!companyRow.IsCustomerIdentityNull())
				company.CustomerIdentity = companyRow.CustomerIdentity;

			if (!companyRow.IsTPIdentityNull())
				companyRow.TPIdentity = companyRow.TPIdentity;


			return company;
		}




		public void UpdateCompany(Company company, IDbTransaction transaction)
		{
			CreateCompany(company, transaction);
		}
		#endregion


		public BillingInfo ReadBillingInfo(string billingId)
		{

			dtoXmlStorage.BillingInfoRow bir = Storage.BillingInfo.FindById(billingId);
			BillingInfo billinginfo = new BillingInfo();

			billinginfo.Company = new Company();
			billinginfo.Company.Id = bir.CompanyId;

			billinginfo.Id = bir.Id;
			if (!bir.IsNameNull())
				billinginfo.Name = bir.Name;
			if (!bir.IsBillingFirstNameNull())
				billinginfo.BillingFirstName = bir.BillingFirstName;
			if (!bir.IsBillingLastNameNull())
				billinginfo.BillingLastName = bir.BillingLastName;
			if (!bir.IsBillingEmailNull())
				billinginfo.BillingEmail = bir.BillingEmail;
			if (!bir.IsBillingCountryNull())
				billinginfo.BillingCountry = bir.BillingCountry;
			if (!bir.IsBillingStreetAddressNull())
				billinginfo.BillingStreetAddress = bir.BillingStreetAddress;
			if (!bir.IsBillingStreetAddress2Null())
				billinginfo.BillingStreetAddress2 = bir.BillingStreetAddress2;
			if (!bir.IsBillingPostalCodeNull())
				billinginfo.BillingPostalCode = bir.BillingPostalCode;
			if (!bir.IsBillingCityNull())
				billinginfo.BillingCity = bir.BillingCity;
			if (!bir.IsBillingCityNull())
				billinginfo.BillingCurrency = bir.BillingCurrency;
			if (!bir.IsTPIdentityNull())
				billinginfo.TPIdentity = bir.TPIdentity;
			if (!bir.IsCustomerIdentityNull())
				billinginfo.CustomerIdentity = bir.CustomerIdentity;

			return billinginfo;

		}
	}
}